#!/bin/bash
 
source make_sandbox.cfg
 
IFS=$'\n';

echo '# step 1: clear destination folder and database >>> BEGIN';
# ============================================================
# step 1: clear destination folder and database
# ============================================================
echo "removing/recreating ${DESTINATION_FOLDER}";
rm -rf --verbose ${DESTINATION_FOLDER}
mkdir --verbose ${DESTINATION_FOLDER}
 
tables=\
`echo "show tables from ${DESTINATION_DATABASE}" | \
mysql -u${DESTINATION_DATABASE_USER} -p${DESTINATION_DATABASE_PASS} | \
grep -v "Tables_in_${DESTINATION_DATABASE}"`
 
IFS=$' ';
count_tables=`echo ${tables} | cat -n | tail -n 1 | cut -f1`;
IFS=$'\n';
 
echo "removing ${count_tables} tables from destination database ${DESTINATION_DATABASE}";
for table in $tables
do
	echo "    - removing ${table} from destination database ${DESTINATION_DATABASE}";
	echo \
		"drop table ${DESTINATION_DATABASE}.${table}" | \
		mysql -u${DESTINATION_DATABASE_USER} -p${DESTINATION_DATABASE_PASS};
done

echo '# step 1: clear destination folder and database >>> END';
